package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.mapper.CommodityMapper;
import cn.wolfcode.trip.base.query.CommodityQueryObject;
import cn.wolfcode.trip.base.service.ICommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommodityServiceImpl implements ICommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public PageInfo<Commodity> query(CommodityQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Commodity> list = commodityMapper.query(qo);
        return new PageInfo<Commodity>(list);
    }

    @Override
    public void saveOrUpdate(Commodity commodity) {
        commodity.setCreateTime(new Date());
        if (commodity.getId() == null) {
            commodityMapper.insert(commodity);
        } else {
            commodityMapper.updateByPrimaryKey(commodity);
        }
    }

    @Override
    public Commodity getOne(Long id) {
        return commodityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Commodity> types() {
        return commodityMapper.types();
    }
}
